// pages/editAssInfo/editAssInfo.js
import upload from '../../utils/upload.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    url: wx.consts.IMG_URL,
    adminUserInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
    let assId = wx.getStorageSync('adminUserInfo').associationId
    this.getdetail(assId)
  },
  getdetail(id) {
    wx.teamApi.association.detail(id).then(res => {
      this.setData({
        adminUserInfo: res.data.result,
        'adminUserInfo.adminName': res.data.result.name
      })
      console.log(res)
      console.log('32')
      // console.log(this.data.adminUserInfo)

    })
  },
  //上传更新
  save() {

    let data = {
      needApplyFollow: this.data.adminUserInfo.needApplyFollow,
      needApplyJoin: this.data.adminUserInfo.needApplyJoin,
      introduction: this.data.adminUserInfo.introduction,
      avatarUri: this.data.adminUserInfo.avatarUri,
      sicUrl: this.data.adminUserInfo.sicUrl,
      name: this.data.adminUserInfo.adminName
    }
    console.log('44')
    console.log(data)
    wx.teamApi.association.adminUpdateinfo(data).then(res => {
      if (res.data.errcode === 0) {
        wx.showToast({
          title: '保存成功！',
        })
        setTimeout(() => {
          // wx.navigateBack({
          //   delta: 1
          // })
          wx.switchTab({
            url: '/pages/my/my',
          })
        }, 1500)

      } else {
        wx.showToast({
          icon: 'none',
          title: '失败！',
        })
      }
    })
  },
  setName(e) {
    let attr = e.currentTarget.dataset.name
    this.data.adminUserInfo[attr] = e.detail.value
  },
  setValue(e) {
    console.log(e)
    let attr = e.currentTarget.dataset.name
    this.data.adminUserInfo[attr] = e.detail.value
    // console.log(this.data.adminUserInfo)
  },
  //修改管理员头像
  uploadPhoto() {
    var that = this
    // upload.img(1).then(res => {
    //   this.setData({
    //     'adminUserInfo.avatarUri': res[0].Location
    //   })
    // })
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认用原图
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        //上传高清图片
        upload.uploadImg([res.tempFilePaths[0]]).then(upres => {
          that.setData({
            'adminUserInfo.avatarUri': upres[0].Location
          })
        })
        //调用方法，上传缩略图
        that.getCanvasImg(res.tempFilePaths[0])
        
      },
    })
    
  },

  //压缩并获取图片
  getCanvasImg(tempFilePaths) {
    //获取图片信息   
    wx.getImageInfo({
      src: tempFilePaths, //图片链接
      success: (res) => {
        //缩放比例
        var setWidth, setHeight
        if (res.width / res.height >= 336 / 336) {
          if (res.width > 336) {
            setWidth = 336
            setHeight = (res.height * 336) / res.width
          } else {
            setWidth = res.width
            setHeight = res.height
          }
        } else {
          if (res.height > 336) {
            setHeight = 336
            setWidth = (res.width * 336) / res.height
          } else {
            setWidth = res.width
            setHeight = res.height
          }
        }
        const ctx = wx.createCanvasContext('attendCanvasId', this)
        ctx.drawImage(tempFilePaths, 0, 0, setWidth, setHeight)
        ctx.draw(true, () => {
          wx.canvasToTempFilePath({ //把当前画布指定区域的内容导出生成指定大小的图片。
            canvasId: 'attendCanvasId',
            width: setWidth,
            height: setHeight,
            destWidth: setWidth, //指定图片宽度
            destHeight: setHeight, //指定图片高度
            fileType: 'jpg', //指定文件类型
            quality: 1,
            success: (res) => {
              //上传缩略图片
              upload.uploadImg([res.tempFilePath]).then(upres => {
                this.setData({
                  'adminUserInfo.sicUrl': upres[0].Location
                })
              })
            }
          }, this)
        })
      }
    })

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function() {

  // }
})